#include "tools/Reader.h"
#include "tools/Wings.h"
#include <windows.h>

QByteArray Reader::g_reader(QString file){
    QByteArray content;
    if (!file.isEmpty() && file.contains(QRegularExpression("[\\/:a-zA-Z0-9]")))
    {
        QScopedPointer<QFile> InitFile(new QFile(file));
        if(InitFile->open(QFile::ReadOnly))
        {
            content = InitFile->readAll();
        }
        else
        {
            content = NULL;
        }
        InitFile->close();
    }
    return content;
};

void Reader::d_writer(QString file, QByteArray bytedata, QIODevice::OpenMode Wmode){
    QScopedPointer<QFile> InitFile(new QFile(file));
    if(InitFile->open(Wmode)){
        InitFile->write(bytedata);
        InitFile->close();
    }
}

QMap<QString, QString> Reader::g_qsstyle(QString file, QString regexp){
    QString PrimaryKey;
    QMap<QString, QString>RdoMap_String;
    QString content = g_reader(file);
    QStringList Reglist = content.split(QRegularExpression("[\\r\\n]+|[\\n\\r]+"));
    for (int lstindex = 0; lstindex < Reglist.size(); lstindex++){
        QString RowContent = Reglist[lstindex];
        QRegularExpression PrimaryRe(regexp);
        QRegularExpressionMatch Primatch = PrimaryRe.match(RowContent);
        QString SimpleValue = RowContent.simplified();
        if (SimpleValue.size() != 0){
            if (SimpleValue.size() != 0 &&
                Primatch.hasMatch()){
                PrimaryKey = Primatch.captured(1);
            }else{
                if (!PrimaryKey.isEmpty()) {
                    RdoMap_String[PrimaryKey] += SimpleValue;
                }
            }
        }else{
            continue;
        }
    }
    return RdoMap_String;
}

QMap<QString, QMap<QString, QString>> Reader::g_inifile(QString file, QString regexp){
    QString PrimaryKey;
    QMap<QString, QMap<QString, QString>>Rdo_NestMap;
    QString content = QString::fromLocal8Bit(g_reader(file));
    QStringList Reglist = content.split(QRegularExpression("[\\r\\n]+|[\\n\\r]+"));
    for (int lstindex = 0; lstindex < Reglist.size(); lstindex++){
        QString RowContent = Reglist[lstindex];
        QRegularExpression PrimaryRe(regexp);
        QRegularExpressionMatch Primatch = PrimaryRe.match(RowContent);
        QString SimpleValue = RowContent.simplified();
        if (SimpleValue.size() != 0 &&
            Primatch.hasMatch()){
            PrimaryKey = Primatch.captured(1);
        }else{
            if (!PrimaryKey.isEmpty()) {
                int ANttionIndex = SimpleValue.lastIndexOf(";", -1);
                if (ANttionIndex != -1 ){
                    SimpleValue = SimpleValue.mid(0, ANttionIndex);
                }
                int EqualIndex = SimpleValue.indexOf("=", 0);
                if (EqualIndex != -1 ){
                    QString key = SimpleValue.mid(0, EqualIndex);
                    QString value = SimpleValue.mid(EqualIndex + 1, -1);
                    Rdo_NestMap[PrimaryKey].insert(key, value);
                }
            } else {
                continue;
            }
        }
    }
    Rdo_NestMap["ALL"]["TEXT"] = content;
    return Rdo_NestMap;
}

void Reader::d_clipb(const char *DTC){
    HWND Handle = GetClipboardOwner();
    HGLOBAL GLBMMR;
    SIZE_T DTSS = sizeof(Nlwingsing().QMultiByteToWideChar(DTC));
    GLBMMR = GlobalAlloc(GHND, DTSS);
    char *LockMMR = (char*)GlobalLock(GLBMMR);
    memcpy(LockMMR, Nlwingsing().QMultiByteToWideChar(DTC).c_str(), DTSS);
    GlobalUnlock(GLBMMR);
    OpenClipboard(Handle);
    EmptyClipboard();
    SetClipboardData(CF_TEXT, GLBMMR);
    CloseClipboard();
    delete LockMMR;
}

std::string Reader::g_clipb(){
    std::string CLPDT;
    HWND Handle = GetClipboardOwner();
    if (OpenClipboard(Handle) &&
        IsClipboardFormatAvailable(CF_TEXT)){
        HANDLE CLPHD = GetClipboardData(CF_TEXT);
        CLPDT = Nlwingsing().QWideCharToMultiByte((char*)GlobalLock(CLPHD));
        GlobalUnlock(CLPHD);
    }
    EmptyClipboard();
    CloseClipboard();
    return CLPDT;
}










